package com.fengye.security.rest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author fengyexjtu@126.com
 * @date 2022年04月11日 7:37 PM
 */

@RestController
@RequestMapping("/api")
@Slf4j
public class UserResource {

    @GetMapping("/greeting")
    public String greeting() {
        log.debug("执行 api -> greeting");
        return "greeting!";
    }

    @GetMapping("/test")
    public String test() {
        return "test!";
    }

    @GetMapping("/authentication/name")
    public String getAuthenticationName() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    @GetMapping("/authentication")
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @GetMapping("/principal")
    public Object getPrincipalObject() {
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @GetMapping("/principal/injection")
    public Principal getPrincipalByInjection(Principal principal) {
        return principal;
    }

    @GetMapping("/authentication/injection")
    public Authentication getAuthenticationByInjection(Authentication authentication) {
        return authentication;
    }
}
